package com.academic_system.controller;

import com.academic_system.model.StuProject;
import com.academic_system.model.Student;
import com.academic_system.service.StudentService;
import com.academic_system.util.Message;
import com.alibaba.fastjson.JSONArray;
import com.itextpdf.text.DocumentException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 20155887 赵开兰
 * @description 学生控制层
 * @project_name StudentController
 * @package com.academic_system.controller
 
 **/
@Controller
@RequestMapping("/student")
@ResponseBody
@CrossOrigin(origins = "*")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * @param stuNum
     * @Description 根据Student表中的学生学号查询学生个人信息
     * @author 20155887 赵开兰
     * @methodName queryStudentByStuNum
     * @returnType  Message
     **/
    @RequestMapping(value = "/queryStudentByStuNum",method = RequestMethod.GET)
    public Message queryStudentByStuNum(@Param("stuNum")int stuNum){
        return studentService.queryStudentByStuNum(stuNum);
    }

    /**
     * @param stuNum
     * @Description 查找学号是否存在
     * @author 20155887 赵开兰
     * @methodName queryStuNum
     * @returnType Message
     **/
    @RequestMapping(value = "/queryStuNum",method = RequestMethod.GET)
    public Message queryStuNum(@Param("stuNum")int stuNum){
        return studentService.queryStuNum(stuNum);
    }

    /**
     * @param student
     * @Description 增加一个学生（学号+密码）
     * @author 20155887 赵开兰
     * @methodName addStudent
     * @returnType Message
     **/
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public Message addStudent(@RequestBody Student student){
        return studentService.addStudent(student);
    }

    /**
     * @param stuNum
     * @Description 删除学生信息表【警告！这是真删！无特殊情况不可调用此接口！】
     * @author 20155887 赵开兰
     * @methodName deleteStudent
     * @returnType Message
     **/
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.GET)
    public Message deleteStudent(@Param("stuNum")int stuNum){
        return studentService.deleteStudent(stuNum);
    }

    /**
     * @Description 查询全部学生
     * @author 20155887 赵开兰
     * @methodName queryStudent
     * @returnType Message
     **/
    @RequestMapping(value = "/queryStudent",method = RequestMethod.GET)
    public Message queryStudent(){
        return studentService.queryStudent();
    }

    /**
     * @param student
     * @Description 修改学生信息表（姓名，性别，身份证，出生年月，民族，电话，籍贯）
     * @author 20155887 赵开兰
     * @methodName updateStudent
     * @returnType Message
     **/
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public Message updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     * @Description 下载生成学生信息表
     * @author 20155887 赵开兰
     * @methodName download
     * @param
     * @returnType Message
     **/
//    @RequestMapping(value = "/download",method = RequestMethod.GET)
//    public Message download(@Param("stuNum")String stuNum,
//                            @Param("stuName")String stuName,
//                            @Param("stuSex")String stuSex,
//                            @Param("stuIdcardNum")String stuIdcardNum,
//                            @Param("stuBirthday")String stuBirthday,
//                            @Param("stuNation")String stuNation,
//                            @Param("stuPoli")String stuPoli,
//                            @Param("stuPhone")String stuPhone,
//                            @Param("stuNativeplace")String stuNativeplace,
//                            @Param("stuGrade")String stuGrade,
//                            @Param("cet46Grade")String cet46Grade,
//                            @Param("academyName")String academyName
//                            ) throws IOException, DocumentException {
//        System.out.println(stuNum);
//        System.out.println(stuName);
//        System.out.println(stuSex);
//        System.out.println(stuIdcardNum);
//        System.out.println(stuBirthday);
//        System.out.println(stuNation);
//        System.out.println(stuPoli);
//        System.out.println(stuPhone);
//        System.out.println(stuNativeplace);
//        System.out.println(stuGrade);
//        System.out.println(cet46Grade);
//        System.out.println(academyName);
//
//        return studentService.download(stuNum,stuName,stuSex,stuIdcardNum,stuBirthday,stuNation,stuPoli,stuPhone,stuNativeplace,stuGrade,cet46Grade,academyName);
//    }
//
//
//    @RequestMapping(value = "/sendMessage",method = RequestMethod.GET)
//    public Message sendMessage(@Param("phone")String phone,@Param("stuNum")String stuNum,
//                            @Param("stuName")String stuName,
//                            @Param("stuSex")String stuSex,
//                            @Param("stuIdcardNum")String stuIdcardNum,
//                            @Param("stuBirthday")String stuBirthday,
//                            @Param("stuNation")String stuNation,
//                            @Param("stuPoli")String stuPoli,
//                            @Param("stuPhone")String stuPhone,
//                            @Param("stuNativeplace")String stuNativeplace,
//                            @Param("stuGrade")String stuGrade,
//                            @Param("cet46Grade")String cet46Grade,
//                            @Param("academyName")String academyName
//    ) throws IOException {
//        System.out.println(phone);
//        System.out.println(stuNum);
//        System.out.println(stuName);
//        System.out.println(stuSex);
//        System.out.println(stuIdcardNum);
//        System.out.println(stuBirthday);
//        System.out.println(stuNation);
//        System.out.println(stuPoli);
//        System.out.println(stuPhone);
//        System.out.println(stuNativeplace);
//        System.out.println(stuGrade);
//        System.out.println(cet46Grade);
//        System.out.println(academyName);
//
//        //return Message.success();
//        return studentService.sendMessage(phone,stuNum,stuName,stuSex,stuIdcardNum,stuBirthday,stuNation,stuPoli,stuPhone,stuNativeplace,stuGrade,cet46Grade,academyName);
//    }

    @RequestMapping("/findAllStuProject")
    @ResponseBody
    public Object findAllStuProject() {
        List<StuProject> stuProjects = studentService.findAllStuProject();
        for (StuProject stuProject : stuProjects) {
            System.out.println(stuProject);
        }
        return stuProjects;
    }

    @RequestMapping("/findMyProject")
    @ResponseBody
    public Object findMyProject(HttpSession session) {
        Student student = (Student) session.getAttribute("currStu");
        System.out.println(student);
        System.out.println(session.getAttribute("Test"));
        int stuId = student.getStuInfoId();
        System.out.println("stuId:"+stuId);
        List<StuProject> stuProjects = studentService.findMyProject(stuId);
        System.out.println("stuProjects.size():"+stuProjects.size());
        for (StuProject project : stuProjects) {
            System.out.println(project.toString());
        }
        return stuProjects;
    }

    @RequestMapping("/changeMyProject")
    @ResponseBody
    public void changeMyProject(StuProject stuProject, HttpServletResponse response) throws IOException {
        boolean flag = studentService.changeMyProject(stuProject);
        System.out.println(stuProject);
        PrintWriter out = response.getWriter();
        if (flag) {
            out.write("true");
        } else {
            out.write("false");
        }
    }

    @RequestMapping("/removeMyProject")
    @ResponseBody
    public void removeMyProject(StuProject stuProject, HttpServletResponse response) throws IOException {
        boolean flag = studentService.removeMyProject(stuProject);
        System.out.println(stuProject);
        PrintWriter out = response.getWriter();
        if (flag) {
            out.write("true");
        } else {
            out.write("false");
        }
    }

//    @InitBinder
//    public void initBinder(WebDataBinder binder, WebRequest request) {
//        //转换日期
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }
}
